<template>
  <a-modal
    :visible="visible"
    :title="t('edit')"
    :cancelText="t('cancel')"
    :okText="t('submit')"
    @ok="submit"
    @cancel="cancel(false)"
  >
    <a-form
       ref="formRef"
      :model="formState"
      :rules="formRules"
      :label-col="labelCol"
      :wrapper-col="wrapperCol"
    > 
<a-form-item ref="desc" :label="t('desc')" name="desc"><a-input v-model:value="formState.desc" /> </a-form-item>
    </a-form>
  </a-modal>
</template>
<script>
import { useI18n  } from "vue-i18n";
import { message } from 'ant-design-vue';
import { edit } from "@/core/api/module/SysDatabackup";
import { defineComponent, reactive, ref, toRaw, watch } from "vue";
export default defineComponent({
  props: {
    visible: {
      type: Boolean,
    },
    record: {
      type: Object
    }
  },
  emit: ["close"],
  setup(props, context) {
    const { t } = useI18n();
    const state = reactive({
      depts: [],
      showComponent: true,
      showPath: true,
      showCode: false
    });
    
    const formRef = ref();
    
    let formState = reactive({ 
    });

    
    watch(props,(props) => {   
       
     formState.id = props.record.id;
formState.name = props.record.name;
formState.type = props.record.type;
formState.desc = props.record.desc;
     
    })

    const formRules = {
     name: [{ required: true, message:t('name') + t("must input"), trigger: "blur" }], 
    };
 

    const submit = (e) => {
      formRef.value
        .validate()
        .then(() => {
          edit(toRaw(formState)).then((response)=>{
              if(response.code == 0){
                message.success({ content: '保存成功', duration: 1 }).then(()=>{
                  cancel(true);
                });
              }else{
                message.error({ content: response.msg, duration: 1 }).then(()=>{
                  cancel(false);
                });
              }
          });
        })
        .catch(error => {
          console.log('error', error);
        });
    };

     
    const cancel = (e=false) => {
      formRef.value.resetFields();
      context.emit("close", e);
    };
 

    return {
      t,
      state,
      submit, 
      cancel,
      formRef,
      formState,
      formRules, 
      labelCol: { span: 6 },
      wrapperCol: { span: 18 }
    };
  },
});
</script>